$(function() {
	$("#addmenu").on('click', function() {
		
		var arr = [];
		var panduan = 1;
		sendForm($(this));
//		window.location.href = "/admin/menu/index";
	})
})

function sendForm(formId,check,mode) {
	
	if(typeof formId == 'object'){
		var btn = formId;
		form = btn.closest('form');
		btn.addClass('disabled').data('txt',btn.text()).html("<i class='ace-icon fa fa-spinner fa-pulse'></i> 处理中...").prop('disabled',true);
		
	} else {
		form = $("#"+formId);
	}
	
	//检查
	check = check ? check : form.data('check');
	mode = mode ? mode : form.data('mode');
	console.log(checkForm(form))
	if( check && !checkForm(form) ) {
		btn.removeClass('disabled').text( btn.data('txt') ).prop('disabled',false);
		return false;
	}
	console.log('验证成功')
	post_url = form.prop('action');
	if (form.data('ajax')){
//		var vars = $("#" + formId).serialize();
		var vars = new FormData( form[0] );
		console.log(vars)
		$.ajax({
			type : "POST",
			url : post_url,
			data : vars,
			contentType: false,
			processData: false,
//			dataType : "json",
			success : function(res){
				rslt( res , mode)
			}
		});
	} else {
//		$("#" + formId).attr("action", post_url);
		if(!form.prop('method')) form.prop('method','post');
//		form.submit();
	}
}
function checkForm(formId) {
	var check_flag = true;
	
	if(typeof formId == 'object'){
		form = formId;
	} else {
		form = $("#"+formId);
	}
	
	form.find(":input").each(function(i) {
		console.log($(this))
		console.log($(this).data("check"))
		if ($(this).data("check")) {
			var v;
			if( $(this).prop('type') == 'checkbox' || $(this).prop('type') == 'radio'  ) {
				v = $(this).prop('checked');
			} else {
				v = $(this).val();
			}
			if (!validate(v, $(this).data("check"))) {
				$(this).parent().next().html(`<div  style="width:80px;background: red;color: #fff;font-size: 12px;text-align: center;">内容不能为空</div>`)
				$(this).data("msg", $(this).data("msg") ? $(this).data("msg") : $(this).prop("placeholder"));
				if ($(this).data("msg").indexOf("|")) {
					tmp = $(this).data("msg").split("|");
					msgtype = tmp[1];
					msg = tmp[0];
				}
				
				if(msgtype == 'popup') {
					LErr(tmp[0]);
				} else {
//					Tips($(this).data("msg"),$(this),1);
					$(this).focus();
					$(this).closest('label').addClass('has-error');
				}
				//提示
				check_flag = false;
				return check_flag;
			} else {
//				$(this).closest('label').replaceClass('has-error','has-success');
				$(this).parent().next().html('')
			}
		}
	})
	return check_flag;
}

/* 验证数据类型*/
function validate(data, datatype) {
	if (datatype.indexOf("|")) {
		tmp = datatype.split("|");
		datatype = tmp[0];
		data2 = tmp[1];
	}
	switch (datatype) {
		case "require":
			if (!data) {
				return false;
			} else {
				return true;
			}
			break;
		case "email":
			var reg = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
			return reg.test(data);
			break;
		case "mobile":
			var reg = /^1[34578]\d{9}$/g;
			return reg.test(data);
			break;
		case "number":
			var reg = /^[0-9]+\.{0,1}[0-9]{0,3}$/;
			return reg.test(data);
			break;
		case "html":
			var reg = /<...>/;
			return reg.test(data);
			break;
		case "gt":
			return data > data2
			break;
		case "notgt":
			return data <= data2
			break;
		case "lt":
			return data < data2
			break;
		case "notlt":
			return data >= data2
			break;
		case "eqt":
			data2 = $("#" + data2).val();
			return data >= data2
			break;
		case "password":
			var reg = /^.{6,16}$/;
			return reg.test(data);
			break;
		case "username":
			var reg = /^[a-z][0-9a-z]{4,}$/g;
			return reg.test(data);
			break;
		case "nickname":
			var reg = /^.{4,}$/g;
			return reg.test(data);
			break;
		case "code":
			var reg = /^[0-9]{4}$/;
			return reg.test(data);
			break;
	}
}
function goTo(url,newopen) {
	if(url == -1) {
		history.back();
	} else if (url == 0) {
		return false
	}  else if (url == 1) {
		window.location.reload();
	} else if(url) {
		if(newopen){
			window.open(url);
		} else {
			window.location.href = url;
		}
		
	}
}
function rslt( res , mode) {
	
	var newopen = 0;
	var mode = 1;
//	Load(1);
	
	if(res.code) {
		rmsg = res.msg ? res.msg: '操作成功！';
		url = res.url ? res.url : res.data.url;
		if(mode){
			Msg(rmsg);
			goTo(url);
		} else {
		}
		
	} else {
		Err(res.msg ? res.msg: '操作失败，稍候再试');
	}
}
function goTo(url,newopen) {
	if(url == -1) {
		history.back();
	} else if (url == 0) {
		return false
	}  else if (url == 1) {
		window.location.reload();
	} else if(url) {
		if(newopen){
			window.open(url);
		} else {
			window.location.href = url;
		}
		
	}
}
function Msg(msg,func) {
	layer.msg(msg,{icon:1},func);
}
function Err(msg) {
	layer.msg(msg,{icon:2});
}
function Load(close) {
	if(close){
		layer.closeAll('loading');
	} else {
		layer.load(0, {
		  shade: false,
		});
	}
}
//

//function sendForm(arr, idname) {
//	
//	checkForm(arr, idname);
//	var isAjax=true;
//	for(var i=0;i<arr.length;i++){
//		if(arr[i].pd==0){
//			isAjax=false;
//		}
//	}
//	console.log(isAjax)
//	if(isAjax){
//		$.ajax({
//			type : "POST",
//			url : "{:url('add')}",
//			data : arr,
//			success : function(res){
//				window.location.href = "/admin/menu/index";
//			}
//		})
//	}else{
//		
//	}
//}
//
function chang(a){
	if(a.value!=0){
		
		$(a).parent().next().html("")
	}else{
		
	}
}
